import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'My.dart';
import 'My.dart';
import 'Fav.dart';
import 'Video.dart';
import 'color/MyColor.dart';
import 'HomeContent.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'color/MyUserTool.dart';
import 'LoginCtrl.dart';
class Root extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
      ),
      home:MyRootCtrl(title: "home",),
    );
  }
}
class MyRootCtrl extends StatefulWidget{
  MyRootCtrl({Key key, this.title}) : super(key: key);
  final String title;
  @override
  State<StatefulWidget> createState() {
    return MyRootState();
  }
}
class MyRootState extends State<MyRootCtrl> {
  int _tabIndex = 0;
  var tabImages;
  List <Widget>pageList;
  var appBarTitles = ['首页', '发现',"视频",'我的'];
  void initState(){
    super.initState();
    pageList  = [HomeContentCtrl(title: "首页"),FavCtrl(title:"收藏",height: 0,),VideoCtrl(title: "视频",),MyCtrl(title: "我的")];
  }
  BottomNavigationBarItem buildTab(String name,String normalIcon, String selectedIcon) {
    return BottomNavigationBarItem(
        icon: ImageHelper.icon(normalIcon),
        activeIcon: ImageHelper.icon(selectedIcon),
        title: Text(name));
  }
  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context,width: 750,height: 1334);
    return Scaffold(
        body:IndexedStack(index: _tabIndex,children:pageList),
        bottomNavigationBar: new BottomNavigationBar(
          items: <BottomNavigationBarItem>[
            buildTab("首页", "ic_tab_home_n.png", "ic_tab_home_s.png"),
            buildTab("收藏", "ic_tab_collect_n.png", "ic_tab_collect_s.png"),
            buildTab("视频", "ic_tab_wx_n.png", "ic_tab_wx_s.png"),
            buildTab("我的", "ic_tab_mine_n.png", "ic_mine_s.png"),
          ],
          type: BottomNavigationBarType.fixed,
          //默认选中首页
          currentIndex: _tabIndex,
          iconSize: 24.0,
          //点击事件
          onTap: (index) {
            if (index == 3){
            //  needLogin();
            }
            setState(() {
              _tabIndex = index;
            });
          },
        ));
  }
  void needLogin(){
    MyUserTool.login().then((success){
      if (success == false){
        Navigator.push(
            context,
            new MaterialPageRoute(fullscreenDialog: true,
                builder: (context) => new LoginCtrl(title:"登录",)));
      }
    });
  }
}